package org.model.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import org.model.common.aspect.SelectOneAspect;
import org.model.common.handler.DynamicTableNameHandler;
import org.model.common.injector.CommonInjector;
import org.model.common.interceptor.FullScanInterceptor;
import org.model.common.mybatis.MysqlKeyGenerator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;

/**
 * MybatisPlusAutoConfig.
 *
 * @author zenghao
 */
@Configuration
@EnableConfigurationProperties(MpExtProperties.class)
public class MybatisPlusAutoConfig {

    @Resource
    private MpExtProperties mpExtProperties;

    /**
     * 分页器、乐观锁.
     *
     * @return PaginationInterceptor
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        // 乐观锁
        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        // 阻断解析器,防止全表更新与删除
        mybatisPlusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        // 分页配置
        //mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        // 自定义拦截器
        mybatisPlusInterceptor.addInnerInterceptor(new FullScanInterceptor(mpExtProperties.getDisableFullScanTable()));
        // 动态表名
        final MpExtProperties.DynamicTableProperties dynamicTableProperties = mpExtProperties.getDynamicTable();
        if (dynamicTableProperties.isEnable()) {
            DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
            dynamicTableNameInnerInterceptor.setTableNameHandler(new DynamicTableNameHandler(dynamicTableProperties.getTables()));
            mybatisPlusInterceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        }
        return mybatisPlusInterceptor;
    }

    /**
     * selectOneAspect.
     *
     * @return SelectOneAspect
     */
    @Bean
    @ConditionalOnMissingBean
    public SelectOneAspect selectOneAspect() {
        return new SelectOneAspect();
    }

    /**
     * sqlInjector.
     *
     * @return CommonInjector
     */
    @Bean
    public CommonInjector sqlInjector() {
        return new CommonInjector();
    }

    /**
     * 主键生成器 需要数据库存在 `id_seq` 表和  next_val 函数（创建语句见注释）.
     *
     * @return IKeyGenerator
     */
    @Bean
    @ConditionalOnMissingBean
    public IKeyGenerator keyGenerator() {
        return new MysqlKeyGenerator();
    }

}
